/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-22 17:04:53
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-28 17:42:30
 * @FilePath: /D_C_lib/thread_safety/pthread_cond_init.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<cstdio>
#include<unistd.h>
#include<pthread.h>

pthread_mutex_t lock;
pthread_cond_t cond;
void* _start_routine(void* args)
{
    pthread_detach(pthread_self());
    const char* name = static_cast<const char*>(args);
    std::cout << "name:" << name << "...run" << std::endl;
    while(true)
    {
        pthread_cond_wait(&cond,&lock);
        std::cout << "name:" << name << "pthread_cond_wait...." << std::endl;
    }
    //return nullptr;
}

int main()
{
    pthread_mutex_init(&lock,nullptr);
    pthread_cond_init(&cond,nullptr);
    pthread_t tid1,tid2,tid3;
    pthread_create(&tid1,nullptr,_start_routine,(void*)"thread 1");
    pthread_create(&tid2,nullptr,_start_routine,(void*)"thread 2");
    pthread_create(&tid3,nullptr,_start_routine,(void*)"thread 3");

    //pthread_join(tid1,nullptr);
    //pthread_join(tid1,nullptr);
    //pthread_join(tid1,nullptr);
    while(true)
    {
        getchar();
        pthread_cond_signal(&cond);
        //pthread_cond_broadcast(&cond);
    }
    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&cond);
    return 0;
}